package com.xphuong.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xphuong.model.Topic;
import com.xphuong.newskios.R;

public class TopicListAdapter extends BaseAdapter {

	LayoutInflater inflater;
	private ArrayList<Topic> mList;
	private Context mContext;

	public ArrayList<Topic> getList() {
		return mList;
	}

	public TopicListAdapter(Context ctx, String[] topicNames,
			String[] topicLinks) {
		mContext = ctx;
		inflater = LayoutInflater.from(mContext);

		mList = new ArrayList<Topic>();
		String[] names = topicNames;
		String[] links = topicLinks;

		for (int i = 0; i < names.length; i++) {
			Topic np = new Topic(names[i], links[i]);
			mList.add(np);
		}
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = (ViewGroup) inflater.inflate(
					R.layout.newspaper_list_item, null);
			holder = new ViewHolder();

			holder.captionTv = ((TextView) convertView
					.findViewById(R.id.caption_tv));
			holder.titleTv = ((TextView) convertView
					.findViewById(R.id.title_tv));

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Topic np = (Topic) getItem(position);
		Typeface typeFace = Typeface.createFromAsset(mContext.getAssets(),
				"RobotoLight.ttf");

		holder.captionTv.setTypeface(typeFace);
		holder.titleTv.setTypeface(typeFace);

		holder.titleTv.setText(np.getTitle());
		holder.captionTv.setText(np.getTitle().substring(0, 1));

		return convertView;
	}

	static class ViewHolder {
		TextView captionTv;
		TextView titleTv;
	}

}
